<template>
   <keep-alive>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #chanSelect="{ model, field }">
        <n-select v-model:value="model[field]" :options="registChanArray"/>
      </template>
    </BasicForm>
   
    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="2048"
    >

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  
  </n-card>
</keep-alive>
</template>

<script lang="ts" setup>
  import {computed, h, onMounted, reactive, ref, unref} from 'vue';
  import {useDialog, useMessage} from 'naive-ui';
  import {BasicTable, TableAction} from '@/components/Table';
  import {BasicForm, FormSchema, useForm} from '@/components/Form/index';
  import {getTableList} from '@/api/table/list';
  import {columns, StatusSwitchAction, registerHandler} from './columns';
  import {PlusOutlined} from '@vicons/antd';
  import {useRouter} from 'vue-router';
  import {getAnchorDaily} from "@/api/liveBroadcast/anchorDaily";
  import {formatToDate} from "@/utils/dateUtil";
  import {comment} from "postcss";



  const registChanArray = ref([]);
  const schemas: FormSchema[] = [
    {
      field: 'anchorId',
      component: 'NInput',
      label: '主播ID',
      componentProps: {
        placeholder: '主播ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'anchorName',
      component: 'NInput',
      label: '主播名称',
      componentProps: {
        placeholder: '主播名称',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'chanId',
      component: 'NInput',
      label: '渠道标识',
      componentProps: {
        placeholder: '渠道标识',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'createTime',
      component: 'NDatePicker',
      label: '直播时间',
      componentProps: {
        placeholder: '直播时间',
        type:"datetimerange",

        onInput: (e: any) => {
          console.log(e);
        },
      },
    }
  ];


  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const dialog = useDialog();
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    anchorId: 0,
    anchorName:'',
    chanId:'',
    createTime:[0,0]
  });

  const params = ref({
    anchorId: 0,
    anchorName:'',
    chanId:'',
    createTime:[0,0]
  });



  const actionColumn = reactive({
    width: 100,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '详情',
            onClick: handleDetail.bind(null, record),
            ifShow: () => {
              return true;
            },
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });


  const [register, {}] = useForm({
    gridProps: {cols: '1 s:1 m:2 l:3 xl:4 2xl:4'},
    labelWidth: 80,
    schemas,
  });

  function addTable() {
    showModal.value = true;
  }

  const loadDataTable = async (res) => {
    if (!formParams.anchorId) {
      formParams.anchorId = 0;
    } else {
      formParams.anchorId = parseInt(formParams.anchorId);
    }
    if (!formParams.chanId) {
      formParams.chanId = '';
    } 
    if (!formParams.anchorName) {
      formParams.anchorName = '';
    } 
    if (!formParams.createTime) {
      formParams.createTime = [0,0];
    } 
    const param = {...formParams, ...res};
    console.log(param);
    delete param.makeDate;
    return await getAnchorDaily(param);
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  

  function handleDetail(record: Recordable) {
    console.log('点击了详情', record);
    router.push({ path: '/liveBroadcast/anchorDailyRecords/'+record.chanId+'/'+record.anchorName, params: { id: record.chanId,name:record.anchorName } });
  }


  function handleSubmit(values: Recordable) {
    console.log("submit",values);
    let tempParams = unref(formParams);
    tempParams = Object.assign(tempParams, values);
    if (values.makeDate) {
      let [startTime, endTime]: [number, number] = values.makeDate;
      let startTimeS = formatToDate(new Date(startTime));
      let endTimeS = formatToDate(new Date(endTime));
      tempParams = Object.assign(tempParams, {startTime: startTimeS, endTime: endTimeS})
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  onMounted(async () => {
    // registerHandler(ItemTableSwitchActions);
    // const chanList = await getChanList();
    // registChanArray.value = chanList.map((chan) => {
    //   return {
    //     label: chan.chanName,
    //     value: chan.chanId
    //   }
    // });
  });
</script>

<style lang="less" scoped></style>
